package com.whjfxx.terminal.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whjfxx.terminal.entity.SysConfig;
import com.whjfxx.terminal.mapper.SysConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by b.c on 2022/4/19 9:33
 */
@Component
public final class SysConfigration {

    /**
     * mapper
     */
    static SysConfigMapper sysConfigMapper;

    /**
     * set ioc
     * @param sysConfigMapper
     */
    @Autowired
    public void setSysConfigMapper(SysConfigMapper sysConfigMapper) {
        SysConfigration.sysConfigMapper = sysConfigMapper;
    }

    /**
     * 初始化
     */
    @PostConstruct
    public static void init() {
        load();
    }

    /**
     * 刷新数据
     */
    public static void refresh() {
        CONFIG_MAP.clear();
        load();
    }


    /**
     * load
     */
    private static void load() {
        LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SysConfig::getConfig, "system");
        List<SysConfig> sysConfigs = sysConfigMapper.selectList(wrapper);
        if (null == sysConfigs) {
            sysConfigs = Collections.emptyList();
        }
        Map<String, String> map = convert2Map(sysConfigs);
        required(map);
        CONFIG_MAP.putAll(map);
    }

    /**
     * convert to map
     * @param sysConfigs
     * @return
     */
    private static Map<String, String> convert2Map(List<SysConfig> sysConfigs) {
        return sysConfigs.parallelStream().collect(Collectors.toMap(a -> a.getCode().toLowerCase(), SysConfig::getValue));
    }


    /**
     * valid or set default
     * @param sysConfigMap
     */
    private static void required(Map<String, String> sysConfigMap) {

        SysRequired[] sysRequireds = SysRequired.values();
        if (0 == sysRequireds.length) {
            return;
        }

        for (SysRequired sysRequired : sysRequireds) {
            String value = sysConfigMap.get(sysRequired.name().toLowerCase());
            if (Objects.nonNull(value)) {
                continue;
            }
            if (Objects.isNull(sysRequired.defaultValue)) {
                throw new java.lang.Error("系统配置项缺少 " + sysRequired.name());
            }
            sysConfigMap.put(sysRequired.name(), sysRequired.defaultValue);
        }

    }

    /**
     * config object
     *
     */
    private static final Map<String, String> CONFIG_MAP = new HashMap<>(32);

    /**
     * get system config
     * @param key
     * @return
     */
    public static String get(String key) {
        return CONFIG_MAP.get(key);
    }


    /**
     * 系统配置校验项目，
     * 根据 name 校验系统配置中是否存在配置，并且设置默认
     */
    enum SysRequired {

        secret("secret"),
        system_name,
        ;

        SysRequired() {}

        SysRequired(String defaultValue) {
            this.defaultValue = defaultValue;
        }

        /**
         * 默认值
         */
        String defaultValue;

    }
}
